package com.example.service.impl;

import com.example.mapper.ApartmentMapper;
import com.example.mapper.CollectionMapper;
import com.example.pojo.Apartment;
import com.example.pojo.Collection;
import com.example.service.CollectionService;
import com.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private ApartmentMapper apartmentMapper;

    @Override
    public void addConllection(Collection collection, HttpServletRequest request) {
        Integer id = collection.getIdA();
        Apartment apartment = apartmentMapper.getApartmentId(id);
        collection.setName(apartment.getName());
        collection.setArea(apartment.getArea());
        collection.setRoomType(apartment.getRoomType());
        collection.setPrice(apartment.getPrice());
        collection.setDate(LocalDateTime.now());
        collection.setImage(apartment.getImage());
        collection.setIdU(JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class));
        collectionMapper.addConllection(collection);
    }

    @Override
    public void delCollection(Integer id, HttpServletRequest request) {
        Integer idU = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        collectionMapper.delCollection(id,idU);
    }

    @Override
    public List<Collection> getCollection(String name, LocalDateTime date, HttpServletRequest request) {
        Integer idU = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        List<Collection> list=collectionMapper.getCollection(name,date,idU);
        return list;
    }
}
